package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.SdParticipantConfDao;
import com.gccloud.design.dao.SdRepoParticipantConfigDao;
import com.gccloud.design.dto.SdRepoParticipantConfDto;
import com.gccloud.design.entity.SdRepoParticipantConfEntity;
import com.gccloud.design.feign.SysRoleGroupClient;
import com.gccloud.design.service.SdParticipantConfService;
import com.gccloud.design.utils.ConstantUtil;
import com.gccloud.design.vo.SdRepoParticipantConfVo;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.UserOrgInfoVO;
import com.gccloud.starter.feign.api.service.ISysUserService;
import com.gccloud.starter.mybatis.page.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wu.yumin
 * @version 1.0.0
 * @Description
 * @date 2021/6/29 15:28
 */
@Slf4j
@Service
public class SdParticipantConfServiceImpl extends ServiceImpl<SdParticipantConfDao, SdRepoParticipantConfEntity> implements SdParticipantConfService {
    @Resource
    private SdRepoParticipantConfigDao sdRepoParticipantConfigDao;

    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private SysRoleGroupClient sysRoleGroupClient;

    @Override
    public boolean batchSaveParticipant(List<SdRepoParticipantConfEntity> list) {
        return saveBatch(list);
    }

    /**
     * 根据环节id删除环节参与人
     */
    @Override
    public boolean deleteParticipantByRepoId(String repoId) {
        UpdateWrapper<SdRepoParticipantConfEntity> updateWrapper = new UpdateWrapper();
        updateWrapper.set("DEL_FLAG", 1).eq("ACTIVITY_REPO_ID", repoId);
        try {
            return update(updateWrapper);
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return false;
        }
    }

    /**
     * 分页查询环节参与者
     */
    @Override
    public Map<String, Object> getParticipantByRepoId(String repoId, String activityName, PageVO pageObj) {
        try {
            log.info("========================后台接收的数据为:repoId {}========================" + repoId);
            Page<SdRepoParticipantConfVo> page = new Page<>(pageObj.getCurrent(), pageObj.getSize());
            List<SdRepoParticipantConfVo> list = sdRepoParticipantConfigDao.getParticipantByRepoId(repoId, activityName, page);
            Map<String, Object> map = new HashMap<>(4);
            map.put("list", list);
            map.put("total", page.getTotal());
            return map;
        } catch (Exception e) {
            log.error("========================查询环节参与人失败,异常信息 {}========================" + e.getMessage());
            throw e;
        }
    }

    /**
     * 查询环节参与者
     */
    @Override
    public List<SdRepoParticipantConfVo> getParticipantListByRepoId(String repoId, String activityName) {
        try {
            log.info("========================后台接收的数据为:repoId {}========================" + repoId);
            return sdRepoParticipantConfigDao.getParticipantByRepoId(repoId, activityName);
        } catch (Exception e) {
            log.error("========================查询环节参与人失败,异常信息 {}========================" + e.getMessage());
            throw e;
        }
    }

    /**
     * 添加环节参与者
     */
    @Override
    public boolean saveParticipant(List<SdRepoParticipantConfDto> sdRepoParticipantConfDtos) {
        try {
            Integer result = sdRepoParticipantConfigDao.saveParticipant(sdRepoParticipantConfDtos);
            return result > 0;
        } catch (Exception e) {
            log.error("========================保存环节参与人失败,异常信息 {}========================" + e.getMessage());
            throw e;
        }
    }

    /**
     * 修改环节参与者
     *
     * @param sdRepoParticipantConfDto 环节参与者实体
     */
    @Override
    public boolean updateParticipant(SdRepoParticipantConfDto sdRepoParticipantConfDto) {
        try {
            return sdRepoParticipantConfigDao.updateParticipant(sdRepoParticipantConfDto) > 0;
        } catch (Exception e) {
            log.error("====================修改环节参与人失败,异常信息 {}====================" + e.getMessage());
            throw e;
        }
    }

    /**
     * 删除环节参与者，根据环节参与人id
     */
    @Override
    public boolean removeParticipantConfByParticipantConfId(Integer participantConfId) {
        try {
            Integer result = sdRepoParticipantConfigDao.removeParticipantConfByParticipantConfId(participantConfId);
            return result > 0;
        } catch (Exception e) {
            log.error("====================删除环节参与人失败,异常信息 {}====================" + e.getMessage());
            throw e;
        }
    }

    @Override
    public List<SdRepoParticipantConfVo> getStaffListByRepoId(String repoId, String activityName) {
        List<SdRepoParticipantConfVo> staffList = new ArrayList<>();
        List<SdRepoParticipantConfVo> list = sdRepoParticipantConfigDao.getParticipantByRepoId(repoId, activityName);
        for (SdRepoParticipantConfVo vo : list) {
            if ("STAFF".equals(vo.getParticipantType())) {
                staffList.add(vo);
            }
            if ("ORG".equals(vo.getParticipantType())) {
                R<List<UserOrgInfoVO>> orgResp = iSysUserService.orgAllUser(vo.getParticipantId());
                orgResp.getData().forEach(entity -> {
                    SdRepoParticipantConfVo el = new SdRepoParticipantConfVo();
                    el.setParticipantId(entity.getUserName());
                    el.setParticipantName(entity.getRealName());
                    el.setParticipantOrgId(Long.parseLong(vo.getParticipantId()));
                    el.setParticipantOrgName(entity.getOrgName());
                    el.setParticipantOrgIdSeq(entity.getIdSeq());
                    el.setParticipantOrgNameSeq(entity.getNameSeq());
                    el.setFinishRule(vo.getFinishRule());
                    el.setIsNotice(vo.getIsNotice());
                    el.setNoticeType(vo.getNoticeType());
                    el.setParticipantType(vo.getParticipantType());
                    staffList.add(el);
                });
            }
            if ("ROLE".equals(vo.getParticipantType()) || "STATION".equals(vo.getParticipantType())) {
                if (list.size() != 0) {
                    R<List<UserOrgInfoVO>> havePermissionUser = sysRoleGroupClient.getUserOrgInfoByRoleGroup(String.valueOf(vo.getParticipantId()));
                    List<UserOrgInfoVO> list1 = havePermissionUser.getData();
                    if (list1.size() != 0) {
                        list1.forEach(item -> {
                            SdRepoParticipantConfVo el = new SdRepoParticipantConfVo();
                            el.setParticipantId(item.getUserName());
                            el.setParticipantName(item.getRealName());
                            el.setParticipantOrgId(Long.valueOf(item.getOrgId()));
                            el.setParticipantOrgName(item.getOrgName());
                            el.setParticipantOrgIdSeq(item.getIdSeq());
                            el.setParticipantType(ConstantUtil.ROLE);
                            el.setFinishRule(ConstantUtil.SINGLE);
                            el.setIsNotice("0");
                            el.setActivityRepoId("");
                            el.setNoticeType("");
                            staffList.add(el);
                        });
                    }
                }
            }
        }
        return staffList;
    }
}
